package com.tt.common.util;

import java.net.*;
import java.util.Enumeration;

/**
 * IP地址工具类
 * @author 青青
 */
public class IpUtils
{
    /**
     * 获取服务器IP地址
     * @return 返回服务器的IP
     */
    @SuppressWarnings("rawtypes")
    public static String getServerIp()
    {
        String SERVER_IP = null;
        try
        {
            Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;
            while (netInterfaces.hasMoreElements())
            {
                NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
                ip = (InetAddress) ni.getInetAddresses().nextElement();
                SERVER_IP = ip.getHostAddress();
                if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1)
                {
                    SERVER_IP = ip.getHostAddress();
                    break;
                }
                else
                {
                    ip = null;
                }
            }
        } catch (SocketException e)
        {
            e.printStackTrace();
        }
        return SERVER_IP;
    }

    public static String getLocalIp()
    {
        try
        {
            // 获取的是本地的IP地址
            InetAddress address = InetAddress.getLocalHost();

            // 192.168.0.121
            String hostAddress = address.getHostAddress();

            // 获取的是该网站的ip地址，比如我们所有的请求都通过nginx的，所以这里获取到的其实是nginx服务器的IP地
            InetAddress address1 = InetAddress.getByName("www.wodexiangce.cn");
            String hostAddress1 = address1.getHostAddress();// 124.237.121.122

            // 根据主机名返回其可能的所有InetAddress对象
            InetAddress[] addresses = InetAddress.getAllByName("www.baidu.com");
            for (InetAddress addr : addresses)
            {
                System.out.println(addr);// www.baidu.com/14.215.177.38
                // www.baidu.com/14.215.177.37
            }
        } catch (UnknownHostException e)
        {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取服务器IP
     * @return
     */
    public static String getSerIp()
    {
        String clientIp ="";

        // 根据网卡取本机配置的IP
        //定义网络接口枚举类
        Enumeration<NetworkInterface> allNetInterfaces;
        try
        {
            //获得网络接口
            allNetInterfaces = NetworkInterface.getNetworkInterfaces();

            //声明一个InetAddress类型ip地址
            InetAddress ip = null;

            //遍历所有的网络接口
            while (allNetInterfaces.hasMoreElements())
            {
                NetworkInterface netInterface = allNetInterfaces.nextElement();

                //同样再定义网络地址枚举类
                Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements())
                {
                    ip = addresses.nextElement();

                    //InetAddress类包括Inet4Address和Inet6Address
                    if (ip != null &&(ip instanceof Inet4Address))
                    {
                        if(!ip.getHostAddress().equals("127.0.0.1"))
                        {
                            clientIp= ip.getHostAddress();
                        }
                    }
                }
            }
        } catch (SocketException e)
        {
            e.printStackTrace();
        }
        return clientIp;
    }
}
